What is array-back?
The array-back npm package is designed to ensure that any value provided to it is returned as an array. This can be useful when you are not sure if a function will receive an argument as a single value or an array of values. It helps to normalize the input for further processing.
What are array-back's main functionalities?
Convert a non-array value to an array
This feature takes a single value and wraps it in an array. If the value is already an array, it returns the array unchanged.
[].concat(arrayBack(value))
Flatten nested arrays
This feature can be used to flatten nested arrays into a single-level array.
arrayBack([1, [2, [3]]])
Ignore empty slots in sparse arrays
This feature allows the conversion of sparse arrays into compact arrays without empty slots.
arrayBack(new Array(5))
Other packages similar to array-back
lodash
Lodash is a comprehensive utility library that provides a wide range of functions for working with arrays, objects, strings, etc. It includes methods like _.toArray which can convert values to arrays, similar to array-back, but it also offers a much larger set of utilities for different purposes.
arrify
Arrify is a minimalistic package that converts a non-array value into an array. It is similar to array-back in its basic functionality but does not offer additional features like flattening nested arrays or handling sparse arrays.
array-back
Example
var arrayify = require("array-back")
arrayify(input) ⇒ Array
⏏
Takes any input and guarantees an array back.
- converts array-like objects (e.g.
arguments
) to a real array - converts
undefined
to an empty array - converts any another other, singular value (including
null
) into an array containing that value - ignores input which is already an array
Kind: Exported function
Param | Type | Description |
---|
input | * | the input value to convert to an array |
Example
> a.arrayify(undefined)
[]
> a.arrayify(null)
[ null ]
> a.arrayify(0)
[ 0 ]
> a.arrayify([ 1, 2 ])
[ 1, 2 ]
> function f(){ return a.arrayify(arguments); }
> f(1,2,3)
[ 1, 2, 3 ]
© 2015-16 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.